package controllers

import (
	"github.com/astaxie/beego"
	"datapro/models"
	"HTdatapro/htmodels"
	"strconv"
	"time"
	"strings"
	"github.com/astaxie/beego/validation"
)

type RecordController struct {
	beego.Controller
}

func (c *RecordController)Prepare() {
	c.Data["uname"]=c.GetSession("uname").(string)
	c.Data["uid"]=c.GetSession("uid").(int)
	c.Data["utype"]=c.GetSession("utype").(int)
}

//带了参数导致bootstrap table无法正常使用
func (c *RecordController)HTRecordList_html() {
	str_Status := c.Ctx.Input.URL()[strings.LastIndex(c.Ctx.Input.URL(), "/") + 1:]
	Status, _ := strconv.Atoi(str_Status)
	c.Data["Status"] = Status
	str1 := c.Ctx.Input.URL()[:strings.LastIndex(c.Ctx.Input.URL(), "/")]
	str_RecordType := str1[strings.LastIndex(str1, "/") + 1:]
	RecordType, _ := strconv.Atoi(str_RecordType)
	c.Data["RecordType"] = RecordType
	c.TplName = "record_list.html"
	c.Render()
}

func (c *RecordController)HTRecordList() {
	Status, _ := c.GetInt("Status")
	RecordType, _ := c.GetInt("RecordType")
	limit, _ := c.GetInt("limit")
	offset, _ := c.GetInt64("offset")
	orderby := ""
	if order := c.GetString("order"); order != "asc" {
		orderby += "-"
	}
	orderfiled := c.GetString("sort")
	orderby += orderfiled
	if orderfiled == "" {
		orderby = "-Id"
	}
	search := c.GetString("search")
	fileds := []string{"Id", "Content", "RecordType", "RecordTime", "UserId"}
	Record := []models.Record{}
	htmodels.HTRecordGetList(&Record, fileds, Status, RecordType, offset, limit, search, "Content", orderby)
	count, _ := htmodels.HTRecordGetCount(search, "Content", Status, RecordType, )
	c.Data["json"] = map[string]interface{}{"rows":&Record, "total":count}
	c.ServeJSON()
}

func (c *RecordController)HTRecordDetail() {
	Id, _ := c.GetInt64("Id")
	if Id != 0 {
		c.Data["update"] = true
		SetRecordDetailInfo(c, Id)
	}
	mytype, _ := c.GetInt("type")
	c.Data["Type"] = mytype
	c.TplName = "record_detail.html"
}

func SetRecordDetailInfo(c *RecordController, Id int64) {
	Record := models.Record{Id:int64(Id)}
	htmodels.HTRecordGetDetail(&Record)
	c.Data["Record"] = &Record
}

func (c *RecordController)HTRecordAdd() {
	Record := models.Record{}
	if err := c.ParseForm(&Record); err != nil {
		Message(&c.Controller, "", "输入数据不对,即将返回！" + err.Error(), "", true)
	}
	Record.RecordTime = time.Now()
	valide := validation.Validation{}
	if b, err := valide.Valid(&Record); !b || err != nil {
		if !b {
			Message(&c.Controller, "", "数据验证失败，即将返回！", "", true)
		}
		if err != nil {
			Message(&c.Controller, "", "数据验证错误，即将返回！" + err.Error(), "", true)
		}
	}
	if err := htmodels.HTRecordAdd(&Record); err != nil {
		Message(&c.Controller, "", "添加失败，即将返回！" + err.Error(), "", true)
	}
	Message(&c.Controller, `/record/detail`, "添加成功！", "", false)
}

func (c *RecordController)HTRecordUpdate() {
	Id, _ := c.GetInt64("Id");
	Record := models.Record{Id:int64(Id)}
	if err := htmodels.HTRecordGetDetail(&Record); err != nil {
		Message(&c.Controller, "", "严重错误，请联系管理员,即将返回！" + err.Error(), "", true)
	}
	if err := c.ParseForm(&Record); err != nil {
		Message(&c.Controller, "", "输入数据不对,即将返回！" + err.Error(), "", true)
	}
	if time_up, err := time.Parse("2006-01-02 15:04:05", c.GetString("recordtime")); err != nil {
		Record.RecordTime = time.Now()
	} else {
		Record.RecordTime = time_up
	}

	valide := validation.Validation{}
	if b, err := valide.Valid(&Record); !b || err != nil {
		if !b {
			Message(&c.Controller, "", "数据验证失败，即将返回！", "", true)
		}
		if err != nil {
			Message(&c.Controller, "", "数据验证错误，即将返回！" + err.Error(), "", true)
		}
	}
	if err := htmodels.HTRecordUpdate(&Record); err != nil {
		Message(&c.Controller, "", "更新失败，即将返回！" + err.Error(), "", true)
	}
	Message(&c.Controller, `/record/list_html` + `/` + strconv.Itoa(Record.RecordType) + `/` + strconv.Itoa(Record.Status), "更新成功！", "", false)
}

func (c *RecordController)HTRecordDelete() {
	Ids := make([]int64, 0, 100)
	c.Ctx.Input.Bind(&Ids, "Id")
	count, err := htmodels.HTRecordDelete(Ids)
	if err != nil {
		Json_Message(&c.Controller, 0, "删除失败" + err.Error())
	}
	Json_Message(&c.Controller, 1, "删除" + strconv.FormatInt(count, 10) + "行")
}

func (c *RecordController)HTRecordStatusChange() {
	Ids := make([]int64, 0, 100)
	c.Ctx.Input.Bind(&Ids, "Id")
	Status, err1 := c.GetInt("ChangedStatus")
	if err1 != nil {
		Json_Message(&c.Controller, 0, "更改失败")
	}
	count, err := htmodels.HTRecordStatusChange(Ids, Status)
	if err != nil {
		Json_Message(&c.Controller, 0, "更改失败" + err.Error())
	}
	Json_Message(&c.Controller, 1, "更改" + strconv.FormatInt(count, 10) + "行状态")
}

//hd
func (c *RecordController)HDRecordList() {
	Status := -1
	RecordType := 2
	limit, _ := c.GetInt("limit")
	offset, _ := c.GetInt64("offset")
	orderby := ""
	if order := c.GetString("order"); order != "asc" {
		orderby += "-"
	}
	orderfiled := c.GetString("sort")
	orderby += orderfiled
	if orderfiled == "" {
		orderby = "-Id"
	}
	search := c.GetString("search")
	fileds := []string{"Id", "Content", "RecordType", "RecordTime", "UserId","Status"}
	Record := []models.Record{}
	htmodels.HTRecordGetList(&Record, fileds, Status, RecordType, offset, limit, search, "Content", orderby)
	//count, _ := htmodels.HTRecordGetCount(search, "Content", Status, RecordType, )

	c.Data["Record"] = &Record
	c.TplName = "hd/list_record.html"
}

func (c *RecordController)HDEditList() {
	Status := -1
	RecordType := 1
	limit, _ := c.GetInt("limit")
	offset, _ := c.GetInt64("offset")
	orderby := ""
	if order := c.GetString("order"); order != "asc" {
		orderby += "-"
	}
	orderfiled := c.GetString("sort")
	orderby += orderfiled
	if orderfiled == "" {
		orderby = "-Id"
	}
	search := c.GetString("search")
	fileds := []string{"Id", "Content", "RecordType", "RecordTime", "UserId","Status"}
	Record := []models.Record{}
	htmodels.HTRecordGetList(&Record, fileds, Status, RecordType, offset, limit, search, "Content", orderby)
	//count, _ := htmodels.HTRecordGetCount(search, "Content", Status, RecordType, )

	c.Data["Record"] = &Record
	c.TplName = "hd/list_bianji.html"
}
